#pragma strict
var selectBool : boolean = false;
var balls : GameObject[] = null;
var toDestroy : GameObject[] = null;
var particles : GameObject[] = null;
var notMoving : GameObject[]= null;
var stillMoving : GameObject[] = null;

var lightenColor : Color = Color(0.4, 0.4, 0.4, 0.0);
var tempPosition : Vector3;
var hitColor : Transform;
var canClick : boolean;
var burst : GameObject;

var columnA : GameObject;
var columnB : GameObject;
var columnC : GameObject;
var columnD : GameObject;
var columnE : GameObject;
var columnF : GameObject;
var columnG : GameObject;
var columnH : GameObject;

private var columnAScript : CreateBallA;
private var columnBScript : CreateBallB;
private var columnCScript : CreateBallC;
private var columnDScript : CreateBallD;
private var columnEScript : CreateBallE;
private var columnFScript : CreateBallF;
private var columnGScript : CreateBallG;
private var columnHScript : CreateBallH;

var appleCount : GameObject;
var blackberryCount : GameObject;
var blueberryCount : GameObject;
var lemonCount : GameObject;
var peachCount : GameObject;
var strawberryCount : GameObject;

var totalCount : int;
var score : int;
var multiplier : int;

private var appleScript : ApplesCount;
private var blackberryScript : BlackberriesCount;
private var blueberryScript : BlueberriesCount;
private var lemonScript : LemonsCount;
private var peachScript : PeachesCount;
private var strawberryScript : StrawberriesCount;

function Start (){
	columnAScript = columnA.gameObject.GetComponent("CreateBallA");
	columnBScript = columnB.gameObject.GetComponent("CreateBallB");
	columnCScript = columnC.gameObject.GetComponent("CreateBallC");
	columnDScript = columnD.gameObject.GetComponent("CreateBallD");
	columnEScript = columnE.gameObject.GetComponent("CreateBallE");
	columnFScript = columnF.gameObject.GetComponent("CreateBallF");
	columnGScript = columnG.gameObject.GetComponent("CreateBallG");
	columnHScript = columnH.gameObject.GetComponent("CreateBallH");
	
	appleScript = appleCount.gameObject.GetComponent("ApplesCount");
	blackberryScript = blackberryCount.gameObject.GetComponent("BlackberriesCount");
	blueberryScript = blueberryCount.gameObject.GetComponent("BlueberriesCount");
	lemonScript = lemonCount.gameObject.GetComponent("LemonsCount");
	peachScript = peachCount.gameObject.GetComponent("PeachesCount");
	strawberryScript = strawberryCount.gameObject.GetComponent("StrawberriesCount");
	
	WaitForStopped();
	
	totalCount = 0;
	score = 0;
	multiplier = 1;

}//end start

function Update () {
	if (score > 99999999) {
		score = 99999999;
	}
	if (score < 0) {
		score = 0;
	}
	
	StaticVars.score = score;
	
	
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	var hit : RaycastHit;   		
	
	
	
	if (canClick){
	
	if(Input.GetMouseButtonDown(0)){
	//when the player clicks
	
		//clean up particles
		particles = GameObject.FindGameObjectsWithTag("Particle");
		if (particles != null){
		//Debug.Log(toDestroy.Length);
			for (var par in particles){
				
				if (par != null){
				
				Destroy(par);
					
	 				//yield WaitForSeconds(0.2);
	 
	 				//Destroy(cloneBurst);
				}	
			}
		}
			
			
			
			
		if (Physics.Raycast (ray, hit, 100)) {
		//if the mouse is over something
		//Debug.Log(hit.collider.gameObject.name);
		
			if (hit.collider.gameObject.name == "Blueberry(Clone)"
				|| hit.collider.gameObject.name == "Strawberry(Clone)"
				|| hit.collider.gameObject.name == "Apple(Clone)"
				|| hit.collider.gameObject.name == "Lemon(Clone)"
				|| hit.collider.gameObject.name == "Peach(Clone)"
				|| hit.collider.gameObject.name == "Blackberry(Clone)"){
				//and its a ball:
					
					//check to see if any balls are currently selected
					balls = GameObject.FindGameObjectsWithTag("Selected");
					
					if (balls.length == 0) {
					//no balls are currently selected
						
						//change color, first find color child
						hitColor = hit.collider.transform.Find("Color");
							
							
						//color child found
						if (hitColor != null) {
							//lighten color
							//hitColor.renderer.material.color = hitColor.renderer.material.color + lightenColor;
							//change tag to 'selected'
							hitColor.gameObject.tag = "Selected";
						}
					}//end no balls selected
					
					else {
					//some balls are selected already
						hitColor = hit.collider.transform.Find("Color");
						
						if(hitColor.gameObject.tag == "Selected"){
						//player has selected the same ball twice
					
							//lighten color
							//hitColor.renderer.material.color = hitColor.renderer.material.color - lightenColor;
							//change tag to 'selected'
							hitColor.gameObject.tag = "Not Selected";
						}
						else {
						//different ball is selected
						//Debug.Log(Vector3.Distance(balls[0].transform.position, hit.transform.position));
						
							if (Vector3.Distance(balls[0].transform.position, hit.transform.position) < 17.0){
							//hit ball is neighboring selected ball
							
								
								//store hit ball's position in tempPosition
								tempPosition = hit.transform.position;
								
								//move hit ball to selected ball's position
								hit.transform.position = balls[0].transform.parent.position;
								
								//move selected ball to tempPosition
								balls[0].transform.parent.position = tempPosition;
								
								//update movement tags on both balls
								//hitColor.gameObject.tag = "Moved";
								
								
								//unhightlight all balls
								for (var ball in balls){
									
										//ball.renderer.material.color = ball.renderer.material.color - lightenColor;
										ball.gameObject.tag = "Not Selected";
									
								}
								
								WaitForStopped();
								
							}
							else {
							//hit ball is not neighboring selected ball
								//Debug.Log(balls[0].name);
								//unhightlight all balls
								for (var ball in balls){
									
										//ball.renderer.material.color = ball.renderer.material.color - lightenColor;
										ball.gameObject.tag = "Not Selected";
									
								}
								
								hitColor = hit.collider.transform.Find("Color");
								//highlight hit ball
							//	hitColor.renderer.material.color = hitColor.renderer.material.color + lightenColor;
								//change ball's tag to 'selected'
								hitColor.gameObject.tag = "Selected";
							}
						}
					}	
				}
			}
		
	}
	}
		
}//end update

function DestroyBalls() : IEnumerator
{
	//yield WaitForSeconds(1.0);
	
	
	toDestroy = GameObject.FindGameObjectsWithTag("Destroy");
	
	
	if (toDestroy != null){
	//Debug.Log(toDestroy.Length);
		for (var ball in toDestroy){
			
			if (ball != null){
			
				totalCount++;
				
				if (ball.name == "Apple(Clone)") {
					appleScript.Increase();
					}
				else if (ball.name == "Blackberry(Clone)") {
					blackberryScript.Increase();
					}
				else if (ball.name == "Blueberry(Clone)"){
					blueberryScript.Increase();
					}
				else if (ball.name == "Lemon(Clone)"){
					lemonScript.Increase();
					}
				else if (ball.name == "Peach(Clone)"){
					peachScript.Increase();
					}
				else if (ball.name == "Strawberry(Clone)"){
					strawberryScript.Increase();
					}
				else {
				//do nothing
				}
				
				score = score + 50*multiplier;
				
							
				var cloneBurst = Instantiate(burst, ball.transform.position, burst.transform.rotation);			
				Destroy(ball);
				
 				//yield WaitForSeconds(0.2);
 
 				//Destroy(cloneBurst);
			}	
		}
	}	
}

function DestroyAndRefill(){
	
	
	//use a for loop to repeat this function until combos stop occurring, using 'repeat' as sensor
	//for (var repeat = 1; repeat == 1; repeat = repeat){
	
		//gather all balls using 'not moving' tag (notMoving's are Movement Child of Balls)
		notMoving = GameObject.FindGameObjectsWithTag("Not Moving");
		
		//check all balls for combos using DestroyCheck function on each ball
		for (var notMoves in notMoving) {
			var otherScript: DestroyScript;
			otherScript = notMoves.transform.parent.transform.GetComponent("DestroyScript");
			otherScript.DestroyCheck();
		}
		
		//gather balls to be destroyed
		toDestroy = GameObject.FindGameObjectsWithTag("Destroy");
	
		Debug.Log(toDestroy.Length);
		if (toDestroy.Length != 0){ 
		//balls to destroy
		
			//Destroy Balls
			DestroyBalls();
			
			yield WaitForSeconds(0.7);
			//Refill all Rows
			columnAScript.Refill();
			columnBScript.Refill();
			columnCScript.Refill();
			columnDScript.Refill();
			columnEScript.Refill();
			columnFScript.Refill();
			columnGScript.Refill();
			yield columnHScript.Refill();
			multiplier = multiplier + 1;
			//repeat to check for new combos
			WaitForStopped();
		
		}
		else {
		//no balls to destroy
		canClick = true;
		multiplier = 1;
		Debug.Log("here " + canClick);
		//repeat = 0; //repeating over, exit DestroyAndRefill
		}
	//}
}//DestroyAndRefill

function WaitForStopped() : IEnumerator 
{
//function to check if all balls are stopped
	yield WaitForSeconds(0.5);
	canClick = false;
	
	
		//gather 'Moving' tagged balls, in stillMoving
		stillMoving = GameObject.FindGameObjectsWithTag("Moving");
		
		if (stillMoving.Length == 0){
			//no balls moving, exit WaitForStopped
			//repeat = 0;
			Debug.Log("Stopped!");
			DestroyAndRefill();
		}
		else {
			//balls still moving
			WaitForSeconds(0.2);
		
			Debug.Log("repeat in WaitForStopped");
			//repeat
			WaitForStopped();
		}

}//end WaitForStopped


function DecreaseTotal () 
{
	if (totalCount > 0) {
	
		totalCount = totalCount - 1;
	}
}//end DecreaseTotal